//
//  LCZReadViewController.m
//  PersonalNews
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZReadViewController.h"
#import "LCZReadCell.h"
#import "LCZNextController.h"
#import "LCZColumnCell.h"
#import "LCZnextColumnController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

// 协议, 代理4
@interface LCZReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, LCZReadCellDelegate, LCZColumnCellDelegate>

@property (nonatomic, strong)UISegmentedControl *seg;
@property (nonatomic, strong)LCZBaseView *segView;

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIButton *button;

@property (nonatomic, assign) NSInteger num; /**< 转动到第几个*/




@end

@implementation LCZReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 把导航栏设为不透明
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationItem.titleView.tintColor = [UIColor orangeColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
//    self.segView = [[LCZBaseView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 150) / 2, 25, 150 / 2, 34)];
//    [self.navigationController.view addSubview:self.segView];
//    self.segView.layer.cornerRadius = 5;
    
    self.seg = [[UISegmentedControl alloc]initWithItems:@[@"阅读", @"栏目"]];
    self.seg.tintColor = [UIColor grayColor];
    self.seg.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 25, 150, 34);
    [self.navigationController.view addSubview:self.seg];
    self.seg.selectedSegmentIndex = 0;
    [self.seg addTarget:self action:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];
    
    // collectionView
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(WIDTH, HEIGHT - 64);
    // 设置水平滚
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:flow];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.backgroundColor = [UIColor orangeColor];
    
        // 注册
    [self.collectionView registerClass:[LCZReadCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.collectionView registerClass:[LCZColumnCell class] forCellWithReuseIdentifier:@"columnReuse"];
      
}


// seg点击方法
- (void)segmentAction:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        self.segView.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 25, 150 / 2, 34);
        self.collectionView.contentOffset = CGPointMake(0, 0);
        
    } else {
        self.segView.frame = CGRectMake((self.view.frame.size.width - 150) / 2 + 150 / 2, 25, 150 / 2, 34);
        self.collectionView.contentOffset = CGPointMake(WIDTH, 0);
    }
}

// 实现协议方法Read
- (void)push:(NSString *)str :(ReadRecommend *)read
{
    LCZNextController *nextController = [[LCZNextController alloc]init];
    nextController.replyid = str;
    nextController.read = read;
    [self.navigationController presentViewController:nextController animated:YES completion:^{
        
    }];
    
}
// 实现协议方法Read
- (void)push:(NSString *)tid
{
    LCZnextColumnController *columnController = [[LCZnextColumnController alloc]init];
//    [self.navigationController pushViewController:columnController animated:YES];
    
    columnController.tid = tid;
    // 模态跳转
    [self.navigationController presentViewController:columnController animated:YES completion:^{
        
    }];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        LCZReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        
        cell.scrollNum = self.num;
        // 签订代理人
        cell.delegate = self;
        
        return cell;
        
    } else {
        LCZColumnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"columnReuse" forIndexPath:indexPath];
        
        // 代理5
        cell.delegate = self;
        
        return cell;
    }
}

// colloctionView滑动的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.num = self.collectionView.contentOffset.x / self.view.frame.size.width;
    
    // 滑动结束刷新数据
    [self.collectionView reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.collectionView) {
        if ( self.num == 0) {
            self.segView.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 25, 150 / 2, 34);
            self.segView.backgroundColor = [UIColor colorWithRed:0.000f green:0.478f blue:1.000f alpha:1.00f];
        }
        if (self.collectionView.contentOffset.x == 0) {
            self.seg.selectedSegmentIndex = 0;
            self.segView.backgroundColor = [UIColor whiteColor];
        }
        if (self.collectionView.contentOffset.x == 375) {
            self.seg.selectedSegmentIndex = 1;
            self.segView.backgroundColor = [UIColor whiteColor];
        }
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
